package subraSoft.centaurus.gui.option;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterJob;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import subraSoft.centaurus.gui.CenApplication;
import subraSoft.centaurus.gui.util.CenImageUtil;
import subraSoft.centaurus.gui.util.CenOpenFileUtil;

/**
 * Dialogo para seleccionar si el reporte se muestra 
 * por pantalla o se imprime directamente.
 * 
 * @author christian
 *
 */
public class CenReportDialog extends JDialog implements ActionListener
{
	private JButton printBtn, pdfBtn;
	private File file;
	private CenImageUtil imagenes = CenImageUtil.getInstance();

	/**
	 * Constructor.
	 * 
	 * @param file referencia al archivo de reporte ya generado.
	 */
	public CenReportDialog(File file, String iconPath) {
		super(CenApplication.frame, "Reporte", true);
		this.file = file;
		setLayout(new BorderLayout(3,3));

		printBtn = new JButton("Imprimir");
		printBtn.setIcon(imagenes.getIcon("img/print.png"));
		printBtn.addActionListener(this);
		pdfBtn = new JButton("Abrir");
		pdfBtn.setIcon(imagenes.getIcon(iconPath));
		pdfBtn.addActionListener(this);

		JPanel panel = new JPanel(new BorderLayout(3,3));
		panel.add(printBtn, BorderLayout.WEST);
		panel.add(pdfBtn, BorderLayout.EAST);
		panel.setBorder(new EmptyBorder(new Insets(10,50,10,50)));

		JLabel l = new JLabel("Seleccione que desea hacer");
		l.setBorder(new EmptyBorder(new Insets(10,10,0,10)));
		add(l, BorderLayout.NORTH);
		add(panel, BorderLayout.SOUTH);

		pack();

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension dv = getSize();
		setLocation( (d.width - dv.width)/2, (d.height - dv.height)/2 );

		setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		CenApplication.setBusyCursor(true);
		if(e.getSource() == pdfBtn)
		{
			CenOpenFileUtil.open(file);
			setVisible(false);
		}
		else if( e.getSource() == printBtn)
		{
			PrinterJob job = PrinterJob.getPrinterJob();
			job.setJobName(file.getAbsolutePath());
			job.printDialog();
			setVisible(false);
		}
		CenApplication.setBusyCursor(false);
	}
}
